<?php

class Transformats {
    /* @create at 2009.11.11-16:24:33
     * @auther gjunming (at) gmail.com
     * xml2array is a function that convert the xml to array
     * @param <SimpleXMLElement> $xml
     * @return array on success or bool false on false; 
     */

    static function xml2array($xml) {
        if ($xml instanceof SimpleXMLElement) {
            $attributes = $xml->attributes();
            $children = $xml->children();
        } else {
            return false;
        }
        //get attributes as items
        if ($attributes) {
            foreach ($attributes as $name => $attribute) {
                $thisNode[$name] = (String) $attribute;
            }
        }
        //get children elements to array item
        if ($children) {
            foreach ($children as $name => $child) {
                if ($child->children()) {
                    $childlength = count($xml->$name);
                    if ($childlength > 1) {
                        $thisNode[$name][] = $this->xml2array($child);
                    } else {
                        $thisNode[$name] = $this->xml2array($child);
                    }
                } else {
                    $thisNode[$name] = (String) $child;
                }
            }
        }

        if (count($thisNode) > 0) {
            return $thisNode;
        } else {
            return false;
        }
    }

}
?>
